package com.ruoyi.zsyh.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.zsyh.domain.ZsyhAgentOrderRel;
import com.ruoyi.zsyh.mapper.ZsyhAgentOrderMapper;
import com.ruoyi.zsyh.domain.ZsyhAgentOrder;
import com.ruoyi.zsyh.service.IZsyhAgentOrderService;
import com.ruoyi.common.core.text.Convert;

/**
 * 大区会长预订单Service业务层处理
 * 
 * @author yuexl
 * @date 2021-12-15
 */
@Service
public class ZsyhAgentOrderServiceImpl implements IZsyhAgentOrderService 
{
    @Autowired
    private ZsyhAgentOrderMapper zsyhAgentOrderMapper;

    /**
     * 查询大区会长预订单
     * 
     * @param id 大区会长预订单ID
     * @return 大区会长预订单
     */
    @Override
    public ZsyhAgentOrder selectZsyhAgentOrderById(String id)
    {
        return zsyhAgentOrderMapper.selectZsyhAgentOrderById(id);
    }

    /**
     * 查询大区会长预订单列表
     * 
     * @param zsyhAgentOrder 大区会长预订单
     * @return 大区会长预订单
     */
    @Override
    public List<ZsyhAgentOrder> selectZsyhAgentOrderList(ZsyhAgentOrder zsyhAgentOrder)
    {
        return zsyhAgentOrderMapper.selectZsyhAgentOrderList(zsyhAgentOrder);
    }

    @Override
    public List<ZsyhAgentOrder> selectZsyhAgentOrderCurrentList(ZsyhAgentOrder zsyhAgentOrder) {
        return zsyhAgentOrderMapper.selectZsyhAgentOrderCurrentList(zsyhAgentOrder);
    }

    @Override
    public List<ZsyhAgentOrder> selectZsyhAgentOrderSum(ZsyhAgentOrder zsyhAgentOrder) {
        return zsyhAgentOrderMapper.selectZsyhAgentOrderSum(zsyhAgentOrder);
    }

    /**
     * 新增大区会长预订单
     * 
     * @param zsyhAgentOrder 大区会长预订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertZsyhAgentOrder(ZsyhAgentOrder zsyhAgentOrder)
    {
        zsyhAgentOrder.setCreateTime(DateUtils.getNowDate());
        int rows = zsyhAgentOrderMapper.insertZsyhAgentOrder(zsyhAgentOrder);
        insertZsyhAgentOrderRel(zsyhAgentOrder);
        return rows;
    }

    /**
     * 修改大区会长预订单
     * 
     * @param zsyhAgentOrder 大区会长预订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateZsyhAgentOrder(ZsyhAgentOrder zsyhAgentOrder)
    {
        zsyhAgentOrder.setUpdateTime(DateUtils.getNowDate());
        zsyhAgentOrderMapper.deleteZsyhAgentOrderRelByOrderId(zsyhAgentOrder.getId());
        insertZsyhAgentOrderRel(zsyhAgentOrder);
        return zsyhAgentOrderMapper.updateZsyhAgentOrder(zsyhAgentOrder);
    }

    /**
     * 修改大区会长预订单
     *
     * @param zsyhAgentOrder 大区会长预订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateZsyhAgentOrderAppLet(ZsyhAgentOrder zsyhAgentOrder)
    {
        zsyhAgentOrder.setUpdateTime(DateUtils.getNowDate());
        return zsyhAgentOrderMapper.updateZsyhAgentOrder(zsyhAgentOrder);
    }

    /**
     * 删除大区会长预订单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZsyhAgentOrderByIds(String ids)
    {
        zsyhAgentOrderMapper.deleteZsyhAgentOrderRelByOrderIds(Convert.toStrArray(ids));
        return zsyhAgentOrderMapper.deleteZsyhAgentOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除大区会长预订单信息
     * 
     * @param id 大区会长预订单ID
     * @return 结果
     */
    @Override
    public int deleteZsyhAgentOrderById(String id)
    {
        zsyhAgentOrderMapper.deleteZsyhAgentOrderRelByOrderId(id);
        return zsyhAgentOrderMapper.deleteZsyhAgentOrderById(id);
    }

    /**
     * 新增大区会长预订单详细信息
     * 
     * @param zsyhAgentOrder 大区会长预订单对象
     */
    public void insertZsyhAgentOrderRel(ZsyhAgentOrder zsyhAgentOrder)
    {
        List<ZsyhAgentOrderRel> zsyhAgentOrderRelList = zsyhAgentOrder.getZsyhAgentOrderRelList();
        String id = zsyhAgentOrder.getId();
        if (StringUtils.isNotNull(zsyhAgentOrderRelList))
        {
            List<ZsyhAgentOrderRel> list = new ArrayList<ZsyhAgentOrderRel>();
            for (ZsyhAgentOrderRel zsyhAgentOrderRel : zsyhAgentOrderRelList)
            {
                zsyhAgentOrderRel.setOrderId(id);
                list.add(zsyhAgentOrderRel);
            }
            if (list.size() > 0)
            {
                zsyhAgentOrderMapper.batchZsyhAgentOrderRel(list);
            }
        }
    }

    public int batchZsyhAgentOrderRel(List<ZsyhAgentOrderRel> list){
        return zsyhAgentOrderMapper.batchZsyhAgentOrderRel(list);
    }

    public Map getMoneyAllByFactoryId(String factoryId){
        return zsyhAgentOrderMapper.getMoneyAllByFactoryId(factoryId);
    }

    public List<Map> getPlatformUserList(String factoryId){
        return zsyhAgentOrderMapper.getPlatformUserList(factoryId);
    }

    public int batchUpdateZsyhAgentOrderRel(List<ZsyhAgentOrderRel> list){
        return zsyhAgentOrderMapper.batchUpdateZsyhAgentOrderRel(list);
    }

    public List<ZsyhAgentOrder> selectZsyhAgentOrderListByFactoryId(ZsyhAgentOrder zsyhAgentOrder){
        return zsyhAgentOrderMapper.selectZsyhAgentOrderListByFactoryId(zsyhAgentOrder);
    }

    public ZsyhAgentOrder selectZsyhAgentOrderDetailsByFactoryId(String id){
        return zsyhAgentOrderMapper.selectZsyhAgentOrderDetailsByFactoryId(id);
    }
}
